package cn.hyy.FVOnline.service.impl;

import cn.hyy.FVOnline.entity.Goods;
import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.entity.Shop;
import cn.hyy.FVOnline.entity.User;
import cn.hyy.FVOnline.service.ShopService;
import cn.hyy.FVOnline.mapper.ShopMapper;
import cn.hyy.FVOnline.service.UserService;
import cn.hyy.FVOnline.service.GoodsService;
import cn.hyy.FVOnline.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

@Service("shopService")
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public Shop findByUserId(Shop shop) {
        return shopMapper.findByUserId(shop);
    }

    @Override
    public void newShop(Shop shop, MultipartFile file) {

        InputStream is = null;
        FileOutputStream os = null;
        try {

            File hs = new File(Constant.shopHsPath + shop.getUserId() + ".png");

            if (!hs.exists()){
                hs.createNewFile();
            }

            is = file.getInputStream();
            os = new FileOutputStream(hs);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) > 0) {
                os.write(buffer, 0, len);
            }

            shop.setHsPath(Constant.shopHsPath + shop.getUserId() + ".png");

            shopMapper.newShop(shop);

        }catch (Exception e){

        }finally {
            try {
                if (is != null){
                    is.close();
                }
                if (os!=null){
                    os.close();
                }
            }catch (Exception e){}
        }
    }

    @Override
    public BufferedImage findHS(String userId) {

        InputStream is = null;
        BufferedImage bi = null;

        try {


            is = new FileInputStream(Constant.shopHsPath + userId + ".png");

            bi = ImageIO.read(is);

        }catch (Exception e){
        }finally {
            try {
                if (is != null){
                    is.close();
                }
            }catch (Exception e){
            }
        }

        return bi;
    }

    @Override
    public Shop findById(Integer id) {
        return shopMapper.findById(id);
    }

    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;

    @Override
    public PageEntitys findPageShopList(String name, Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        name = "%"+ name +"%";
        List<Shop> pageShopList = shopMapper.findPageShopList(name, start, pageSize);
        for (Shop shop : pageShopList) {
            User user = new User();
            user.setId(shop.getUserId());
            user = userService.findById(user);
            shop.setUser(user);
            PageEntitys pageGoods = goodsService.findPageGoods(shop.getId(), 1, 6);
            List<Goods> goods = pageGoods.getGoods();
            shop.setGoodsList(goods);
        }
        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setShops(pageShopList);
        pageEntitys.setCurrent(page);
        pageEntitys.setTotal(shopMapper.findSearchTotal(name));
        return pageEntitys;
    }

    @Override
    public void delShop(Integer id) {
        shopMapper.delShop(id);
    }


    @Override
    public PageEntitys findPageShopList2(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<Shop> pageShopList = shopMapper.findPageShopList2(start, pageSize);
        for (Shop shop : pageShopList) {
            User user = new User();
            user.setId(shop.getUserId());
            user = userService.findById(user);
            shop.setUser(user);
        }
        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setShops(pageShopList);
        pageEntitys.setCurrent(page);
        pageEntitys.setTotal(shopMapper.findSearchTotal2());
        return pageEntitys;
    }

    @Override
    public void addShop(Integer id) {
        shopMapper.addShop(id);
    }
}
